<?php

class feed_Component_Block_Form_Video extends Phpfox_Component{
	public function process(){
		Phpfox::isUser(true);
		$sModule = $this->request()->get('module', false);
		$iItem =  $this->request()->getInt('item', false);
                $aRows=Phpfox::getService("bible.category")->getAll();
                foreach($aRows as $iKey=>$aRow)
                {
                    $aRows[$iKey]['book']=phpfox::getService("bible.book")->getBook_Category($aRow['category_id']);
                }
                
		Phpfox::getUserParam('video.can_upload_videos', true);

		if ($sModule !== false && $iItem !== false && Phpfox::hasCallback($sModule, 'uploadVideo'))
		{
			$aCallback = Phpfox::callback($sModule . '.uploadVideo', $iItem);
		}

		if (($aVals = $this->request()->get('val')))
		{
			if (($iFlood = Phpfox::getUserParam('video.flood_control_videos')) !== 0)
			{
				$aFlood = array(
					'action' => 'last_post', // The SPAM action
					'params' => array(
						'field' => 'time_stamp', // The time stamp field
						'table' => Phpfox::getT('video'), // Database table we plan to check
						'condition' => 'user_id = ' . Phpfox::getUserId(), // Database WHERE query
						'time_stamp' => $iFlood * 60 // Seconds);
					)
				);

				// actually check if flooding
				if (Phpfox::getLib('spam')->check($aFlood))
				{
					Phpfox_Error::set(Phpfox::getPhrase('video.you_are_sharing_a_video_a_little_too_soon') . ' ' . Phpfox::getLib('spam')->getWaitTime());
				}
			}

			if (Phpfox_Error::isPassed())
			{
				if (Phpfox::getService('video.grab')->get($aVals['url']))
				{
					if ($sTitle = Phpfox::getService('video.process')->addShareVideo($aVals))
					{
						if (Phpfox::getService('video.grab')->hasImage())
						{
							if (isset($aVals['module']) && isset($aVals['item']) && Phpfox::hasCallback($aVals['module'], 'uploadVideo'))
							{
								$aCallback = Phpfox::callback($aVals['module'] . '.uploadVideo', $aVals['item']);

								if ($aCallback !== false)
								{
									$this->url()->send($aCallback['url_home'], array('video', $sTitle), Phpfox::getPhrase('video.video_successfully_added'));
								}
							}

							$this->url()->forward(Phpfox::getService('video')->makeUrl(Phpfox::getUserBy('user_name'), $sTitle), Phpfox::getPhrase('video.video_successfully_added'));
						}
						else
						{
							$this->url()->send('video.edit.photo', array('id' => $sTitle), Phpfox::getPhrase('video.video_successfull_added_however_you_will_have_to_manually_upload_a_photo_for_it'));
						}
					}
				}
			}

			$sModule = (isset($aVals['module']) ? $aVals['module'] : false);
			$iItem =  (isset($aVals['item']) ? $aVals['item'] : false);
		}

		$this->template()->setTitle(Phpfox::getPhrase('video.share_a_video'))
			->setBreadcrumb(Phpfox::getPhrase('video.videos'), $this->url()->makeUrl('video'))
			->setBreadcrumb(Phpfox::getPhrase('video.share_a_video'), $this->url()->makeUrl('video.share'), true)
			->setHeader(array(
					'video.js' => 'module_video'
				)
			)
			->assign(array(
					'sSites' => Phpfox::getService('video.grab')->getSites(),
					'sModule' => $sModule,
					'iItem' => $iItem,
                                        'aRows' => $aRows,
					'sCategories' => Phpfox::getService('video.category')->get(),
					'sEditorId' => $this->request()->get('editor_id')
				)
			);
	}
}